package com.qzh.G.base;

import cn.hutool.core.util.IdUtil;
import com.qzh.G.Equipment;
import com.qzh.G.Item;
import com.qzh.G.utils.RandomUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;
@Data
public class Player extends ActiveObject  {
    //当前使用的武器类型
    private L1Type usedl1Type;
    private L2Type usedl2Type;
    private L3Type usedl3Type;
    private BigDecimal lucky;
    private BigDecimal speed;
    public Player(){
        //default 1
        super(1);
    }

    @Override
    public Attack SingleAttack(Long id) {
                Attack attack = new Attack();
                attack.setAttackType(AttackType.damage);
                attack.setFrom(this.getId());
                attack.setTo(id);
                attack.setUuid(IdUtil.fastUUID());
                attack.setDamageType1(usedl1Type);
                attack.setDamageType2(usedl2Type);
                attack.setDamageType3(usedl3Type);
                attack.setChr(getChr());
                attack.setChd(getChd());
                BigDecimal damage = getAttack();
                //乘以伤害增幅
               attack.setValue( damage.multiply(BigDecimal.ONE.add(getDamageRate())));
                 return attack;
    }

    @Override
    public Attack heal(long id) {
        Attack attack = new Attack();
        attack.setAttackType(AttackType.heal);
        attack.setFrom(this.getId());
        attack.setTo(id);
        attack.setUuid(IdUtil.fastUUID());
        attack.setChr(BigDecimal.valueOf(0));//治疗效果不会暴击
        attack.setChd(getChd());
        BigDecimal damage = getAttack();
        //乘以伤害增幅
        attack.setValue( damage.multiply(BigDecimal.ONE.add(BigDecimal.ZERO)));
        return attack;
    }

    @Override
    public List<Attack> MultiAttack(List[] ids) {
        return super.MultiAttack(ids);
    }

    @Override
    public AttackResult getHurt(Attack attack) {
       boolean isDodge = RandomUtil.ask(getDodge_rate());
       boolean isCH = RandomUtil.ask(attack.getChr());
       BigDecimal damage=new BigDecimal(0);
       if(attack.getAttackType().equals(AttackType.heal)){
           BigDecimal newHp = this.getHp().add(attack.getValue());
           if(newHp.compareTo(getHpLimit())>=0){
               this.setHp(getHpLimit());
           }
           else {
               this.setHp(newHp);
           }

       }
        else{
            if(!isDodge){
                //攻击减防御
                  damage = attack.getValue().subtract(getDefend());
                  //计算伤害减幅
                damage = damage.multiply(BigDecimal.ONE.subtract(getReduceRate()));
                  if(damage.compareTo(BigDecimal.ZERO)<=0){//如果伤害低于0，则设置为0
                          damage =   BigDecimal.ZERO;
                  }
                if(isCH){
                    damage = attack.getChd().multiply(damage);
                       }
                }
                BigDecimal newHp = this.getHp().subtract(damage);
                if(newHp.compareTo(BigDecimal.ZERO)<=0){
                    death();
                }
                else {
                    setHp(newHp);
                }

            }

        AttackResult attackResult = new AttackResult();
        if(isDodge){
            attackResult.setType(AttackResultEnum.MISS);
        }
        if(!isDodge){
            attackResult.setType(AttackResultEnum.SUCCESS);
        }
        attackResult.set_C_H(isCH);
        attackResult.setValue(damage);
        attackResult.setTargetHp(getHp());
       return attackResult;
    }
    void death() {
        System.out.println("player is death");
    }



}
